var _fund_sources = 'fund_sources'; 
$(function(){
	/** 是否展示任务计划 **/
	var _isTaskPaln = $('#isTaskPlan').val();
	if (_isTaskPaln == '') {
		$('#task_plan_tr').hide();
	}
	
	/**资金来源*/
	$.get('dataDict/dataDictsByCode.shtml', {code : _fund_sources}, function(data){
		var checkboxs_html = '';
		for ( var i = 0; i < data.length; i++) {
			checkboxs_html = checkboxs_html
								+ '<input class="easyui-validatebox" required="true" type="checkbox" name="fundSourcesArray" id="fund_sources_' 
								+ data[i].code + '" ';
			if ($('#fundSources_temp').length > 0) {
				var _fundSources_temp = $('#fundSources_temp').val();
				if (data[i].code == _fundSources_temp) {
					checkboxs_html = checkboxs_html + ' checked="checked"';
				}
			}
			checkboxs_html = checkboxs_html + ' value="'
								+ data[i].code + '" style="vertical-align:middle;"><label for="fund_sources_'
								+ data[i].code + '" style="vertical-align:middle;">'
								+ data[i].name + '</label>';
		}
		$('#fund_sources_td').html(checkboxs_html);
	});
	
	/**初始化指标*/
	$('#projectIndexId').combotree({
		url : 'projectIndex/projectIndexs',
		onLoadSuccess : function (node, data) {
			 var t = $(this);
	  		 if(data){
			     $(data).each(function(index,d){
					if(this.state == 'closed'){
					    t.tree('expandAll');
					}
			     });
			}
		},
		required : true,
		tipPosition : 'bottom',
		missingMessage : '请选择所属指标'
	});
	$('#pId_clear_btn').click(function(){
		$('#projectIndexId').combotree('clear');
	});
	
	/** 保存 */
	$('#btnSave').click(function(){
		$('#name').validatebox({
			required : true,
			tipPosition : 'bottom',
			validType : 'length[1,50]',
			missingMessage : '主题不能为空',
			invalidMessage : '主题不能超过50个字符'
		});
		$('#remark').validatebox({
			required : true,
			tipPosition : 'bottom',
			missingMessage : '任务内容不能为空'
		});
		$('#planAmount').numberbox({
			required : true,
			min:0,
			precision : 2,
			tipPosition : 'bottom',
			missingMessage : '计划使用资金不能为空',
			invalidMessage : '最小为0.00，只能输入两位小数'
		})
		$('#startTime').validatebox({
			required : true,
			tipPosition : 'bottom',
			missingMessage : '任务起始时间不能为空'
		});
		$('#endTime').validatebox({
			required : true,
			tipPosition : 'bottom',
			missingMessage : '任务结束时间不能为空'
		});
		var checked_fundsources = $("input[name='fundSourcesArray']:checked");
		if (0 == checked_fundsources.length) {
			$.messager.alert('提醒', '来源资金不可以未空，且只能勾选一个！','info');
			return;
		} else if (1 != checked_fundsources.length) {
			$.messager.alert('提醒', '来源资金只能勾选一个！','info');
			return;
		}
		var task_actor_ids = $('#taskActorIds').val();
		if ('' == task_actor_ids) {
			$.messager.alert('提醒', '任务参与者不能为空','info');
			return;
		}
		var valid = $('#form').form('validate');
		
		if(valid) {
			$("body").mask();
			$('#form').ajaxSubmit({
				url : 'task/save',
				success : function(data) {
					$("body").unmask();
					if (!data.success) {
						$.messager.alert('提醒', data.message,'info');
					} else {
						$.messager.alert('提醒', data.message,'info', function(){
							$('#form').form('clear');
							parent.reSearch();
							parent.closeEasyUiWindow('win');
						});
					}
				}
			});
		}
	});
	
	
	/** 取消 */
	$('#btnCancel').click(function(){
		$('#form').form('clear');
		parent.closeEasyUiWindow('win');
	});
	makeActorSelect();
});

function userSelectCallback(){
	makeActorSelect();
}

function makeActorSelect(){
	//alert($('#taskActorIds').val())
	//if( $('#actorId').val()==''){
		 $('#_actor').html('');
		 $('<select id="_actorId" name="_actorId" onChange="changeActor()" class="edit-input" style="width:250px" ></select>').appendTo("#_actor"); 
		 if($('#taskActorIds').val().length>0){
				var ids = $('#taskActorIds').val().split(',');
				var names = $('#taskActorNames').val().split(',');
				for(var i=0;i<ids.length;i++){
					var tmp = {};
					tmp.id = ids[i];
					tmp.text = names[i];
					if (i == 0) {
						$("<option value='"+ids[i]+"' selected='selected'>"+names[i]+"</option>").appendTo("#_actorId");
						$('#actorName').val(names[i]);
					} else {
						$("<option value='"+ids[i]+"'>"+names[i]+"</option>").appendTo("#_actorId");
					}
				}
			}
	//}else{
	//	$('#_actorId').html('<label>'+$("#acotrName").val()+'</label>');
	//}
	if($('#actorId').val()!=null && $('#actorId').val()!=''){
		$('#_actorId').val($('#actorId').val());
	}
}

function changeActor(){
	$('#actorName').val($("#_actorId").find("option:selected").text()); //获取Select选择的text
	$('#actorId').val($("#_actorId").val()); //获取Select选择的Value
}
